import { Injectable } from "@nestjs/common";
import { MongoClient } from "mongodb";
import * as dotenv from "dotenv";
dotenv.config();
@Injectable()
export class LoggerService {
  private client: MongoClient | null = null;
  async getClient() {
    if (this.client) return this.client;
    this.client = new MongoClient(
      process.env.MONGO_URI || "mongodb://localhost:27017"
    );
    await this.client.connect();
    return this.client;
  }
  async log(collection: string, doc: any) {
    const c = (await this.getClient()).db().collection(collection);
    await c.insertOne({ ...doc, createdAt: new Date() });
  }
}
